﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using Model;
using System.Configuration;
using System.Data;
using System.IO;

namespace TCDC_System.Attribute.xml
{
    /// <summary>
    /// Summary description for DashboardSchema
    /// </summary>
    /// 
    public class DashboardSchema : IHttpHandler
    {
        private DBUtil DB;
        XmlTextWriter objX;

        public void ProcessRequest(HttpContext context)
        {
                this.DB = new DBUtil();
                DB.strConn = ConfigurationManager.AppSettings["ConnString"];

                string sql = "SELECT * FROM tcdc_mst_dashboard " +
                       "WHERE parent_id = '0' ORDER BY sorting_no ";

                DataTable dt = DB.ExecuteReader(sql);

                context.Response.Clear();
                context.Response.ContentType = "text/xml";

                objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
                objX.WriteStartDocument();

                objX.WriteStartElement("groups");

                foreach (DataRow row in dt.Rows)
                {
                    objX.WriteStartElement("group");
                    objX.WriteAttributeString("title", row["description"].ToString());

                    GenerateSub(row["dashboard_id"].ToString());
                    objX.WriteEndElement();
                }

                objX.WriteEndElement();
                objX.WriteEndDocument();
                objX.Flush();
                objX.Close();
                context.Response.End();

        }

        protected void GenerateSub(string parent_id)
        {
            string sql = "SELECT * FROM " +
                         "tcdc_mst_dashboard d, tcdc_mst_function f " +
                         "WHERE d.function_id = f.function_id " +
                         "AND d.parent_id = '" + parent_id + "' " +
                         "ORDER BY d.sorting_no ";

            DataTable dt = DB.ExecuteReader(sql);

            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    objX.WriteStartElement("item");
                    objX.WriteElementString("title", row["description"].ToString());
                    objX.WriteElementString("id", row["function_id"].ToString());
                    objX.WriteElementString("url", row["url"].ToString());


                    string curFile = System.Web.HttpContext.Current.Server.MapPath("//Attribute/images/icons/Favorites/") + row["icon"].ToString();

                    string fileTF = File.Exists(curFile) ? "../Attribute/images/icons/Favorites/" + row["icon"].ToString() : "../Attribute/images/icons/Favorites/icon.png";
                    objX.WriteElementString("item-icon", fileTF);
                   objX.WriteEndElement();
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}